<template>
  <div>

    <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        default-expand-all
        :row-class-name="tableRowClassName"
        class="projectDelay"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column
          prop="date"
          label="日期"
          sortable
          width="180">
      </el-table-column>
      <el-table-column
          prop="name"
          label="姓名"
          sortable
          width="180">
      </el-table-column>
      <el-table-column
          prop="address"
          label="地址">
      </el-table-column>
    </el-table>


    <nav aria-label="Page navigation example">
      <ul class="pagination justify-content-center page-self-style">
        <span class="nums">共 {{ page.total }} 条</span>
        <li class="page-item">
          <div class="page-link" @click="handleLastPage()"><</div>
        </li>
        <li class="page-item">
          <div :class="page.current==1?'page-link active':'page-link'" @click="changeCurrentPage(1)">1</div>
        </li>
        <li class="page-item" v-if="page.current>4&&pageTotal>7">
          <div class="page-link" @click="handleLastFiveCurrent">...</div>
        </li>
        <li class="page-item" v-for="item in pageTotalArr" >
          <div :class="page.current==item?'page-link active':'page-link'" @click="changeCurrentPage(item)">{{item}}
          </div>
        </li>
        <li class="page-item" v-if="pageTotal>7&&page.current+3<pageTotal">
          <div class="page-link"  @click="handleNextFiveCurrent" >...</div>
        </li>
        <li class="page-item" v-if="pageTotal&&pageTotal!=1">
          <div :class="page.current==pageTotal?'page-link active':'page-link'" @click="changeCurrentPage(pageTotal)">{{ pageTotal }}</div>
        </li>
        <li class="page-item">
          <div class="page-link" @click="handleNextPage">></div>
        </li>
      </ul>
    </nav>


    <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>

    <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="date"
          label="日期"
          sortable
          width="180">
      </el-table-column>
      <el-table-column
          prop="name"
          label="姓名"
          sortable
          width="180">
      </el-table-column>
      <el-table-column
          prop="address"
          label="地址">
      </el-table-column>
    </el-table>

  </div>
</template>
<script>
export default {
  name: 'elementui-table',
  components: {},
  data() {
    return {
      data: [{
        label: '一级 1',
        disabled:true,
        children: [{
          label: '二级 1-1',
          disabled:true,
          children: [{
            label: '三级 1-1-1'
          }]
        }]
      }, {
        label: '一级 2',
        children: [{
          label: '二级 2-1',
          children: [{
            label: '三级 2-1-1'
          }]
        }, {
          label: '二级 2-2',
          children: [{
            label: '三级 2-2-1'
          }]
        }]
      }, {
        label: '一级 3',
        children: [{
          label: '二级 3-1',
          children: [{
            label: '三级 3-1-1'
          }]
        }, {
          label: '二级 3-2',
          children: [{
            label: '三级 3-2-1'
          }]
        }]
      }],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      tableData: [{
        id: 1,
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        id: 2,
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        id: 3,
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1519 弄',
        children: [{
          id: 31,
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄',
          children:[
            {
              id: 88,
              date: '2016-05-01',
              name: '王小虎',
              address: '上海市普陀区金沙江路 1519 弄',
            }
          ]
        }, {
          id: 32,
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        }]
      }, {
        id: 4,
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1516 弄'
      }],
      activeIndex: "",
      page: {
        total: 300,
        size: 10,
        current: 7,
      },
      messageData: {
        list: [],
        title:''
      },
    }
  },
  computed: {
    pageTotal() {
      return Math.ceil(this.page.total * 1 / this.page.size * 1)
    },
    pageTotalArr(){
      var arr=[];
      if(this.page.current>0&&this.page.current<5){
        //console.log(1)
        for(var i=2;i<this.pageTotal-1;i++){
          if(arr.length<5){
            arr.push(i)
          }
        }
        // console.log(1,arr)
      }else{
        if(this.page.current+3>this.pageTotal&&this.page.current<this.pageTotal+1){
          for(var i=-5;i<0;i++){
            if(arr.length<5){
              arr.push(this.pageTotal+i)
            }
          }
          // console.log(2,arr)
        }else{
          //console.log(3)
          for(var i=-2;i<this.pageTotal;i++){
            if(arr.length<5){
              arr.push(this.page.current+i)
            }
          }
          // console.log(3,arr)
        }
      }
      console.log(arr)
      return arr;
    },
  },
  watch: {},
  methods: {
    tableRowClassName({row, rowIndex}) {

      if (row.children) {
        return 'success-row';
      }
      // if (rowIndex === 1) {
      //   return 'warning-row';
      // } else if (rowIndex === 3) {
      //   return 'success-row';
      // }
      // return '';
    },
    //改变当前页
    changeCurrentPage(currentPage) {
      this.page = {
        current: currentPage,
        size: this.page.size,
      }
      this.getList(currentPage)
    },
    //上一页
    handleLastPage() {
      if (this.page.current == 1) {
        return false
      }
      this.changeCurrentPage(this.page.current - 1)
    },
    //下一页
    handleNextPage() {
      if (this.page.current == this.pageTotal) {
        return false
      }
      this.changeCurrentPage(this.page.current + 1)
    },
    //上5页
    handleLastFiveCurrent(){
      this.page.current=this.page.current-5;
      this.getList(this.page)
    },
    //下5页
    handleNextFiveCurrent(){
      this.page.current=this.page.current+5;
      this.getList(this.page)
    },
    getList(currentPage){
        this.page.current =  currentPage ;
    },
    handleNodeClick(){

    }
  },


};
</script>

<style lang="less">
.el-table .success-row td {
  border-right: none;
  background-color: #f2f2f2;
  border-top: 1px solid #bfbfbf;
}
.projectDelay.el-table th{padding-top: 0px;padding-bottom: 0px;line-height: 50px}
//
//.projectDelay .el-table__row .el-table__indent {
//  padding-left: 0px !important;
//}
//
//.projectDelay .el-table__row .el-table__placeholder {
//  width: 0px;
//}
</style>